package com.dongline.test.mapjoin;

import com.dongline.test.join.JoinDriver;
import com.dongline.test.join.JoinMapper;
import com.dongline.test.join.JoinReducer;
import com.dongline.test.join.OrderWritable;
import groovyjarjarantlr.StringUtils;
import jodd.util.StringUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class MapJoinMapper extends Mapper<LongWritable, Text,Text,NullWritable> {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {

        Configuration configuration = new Configuration();



        Job job=Job.getInstance(configuration);

        ///这句话很重要必须加上
        job.setJarByClass(MapJoinMapper.class);


        job.setMapperClass(MapJoinMapper.class);
//        job.setReducerClass(JoinReducer.class);


//        job.setMapOutputKeyClass(Text.class);
//        job.setMapOutputValueClass(OrderWritable.class);


        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);


        job.addCacheFile(new URI("file:///D:/chanpin.txt"));



        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));


        //设置reduce为0

        job.setNumReduceTasks(0);
//        job.setCombinerClass();可以设置一个Combiner，功能类似于reducer，但是是运行在maptask上的。maptask自己数据进行分区排序之前调用一次。分区排序后调用一次。业务场景



        boolean re= job.waitForCompletion(true);

        System.exit(re?0:1);
    }

    Map<String,String> map=new HashMap<>();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {


        ///setup实在maptask处理数据之前调用一次
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("chanpin.txt")));
        String line;
        while(StringUtil.isNotEmpty(line= bufferedReader.readLine()))
        {
            String [] strs=line.split(" ");
            map.put(strs[0],strs[1]);

        }
        bufferedReader.close();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line=value.toString();
        String[] str=line.split(" ");

        String fild=map.get(str[1]);

        String result=str[0]+" "+str[1]+" "+fild+" "+str[2];
        context.write(new Text(result),NullWritable.get());
    }
}
